<?php


namespace app\admin\controller\order;


use app\common\controller\Adminbase;
use EasyWeChat\Factory;
use think\Db;
use think\db\Where;

class Morder extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[] = ['o.is_del', '=', 0];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['order_no'])) {
                    $where[] = ['o.order_no', 'like', '%' . $filter['order_no'] . '%'];
                }
                if (isset($filter['status'])) {
                    $where[] = ['o.status', '=', $filter['status']];
                }
                if (isset($filter['pay_status'])) {
                    $where[] = ['o.pay_status', '=', $filter['pay_status']];
                }
                if (isset($filter['nickname'])) {
                    $where[] = ['m.nickname', 'like', '%' . $filter['nickname'] . '%'];
                }

                if (isset($filter['province'])) {
                    $province = Db::name('area')->find($filter['province']);
                    $where[] = ['b.province', '=', $province['name']];
                }
                if (isset($filter['city'])) {
                    $city = Db::name('area')->find($filter['city']);
                    $where[] = ['b.city', '=', $city['name']];
                }

                if (isset($filter['business_name'])) {
                    $where[] = ['b.business_name', 'like', '%' . $filter['business_name'] . '%'];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);

                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }

                    $where[] = ['o.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['pay_time'])) {
                    $pay_time = explode(' - ', $filter['pay_time']);

                    foreach ($pay_time as $k => $v) {
                        $pay_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.pay_time', 'between', [$pay_time[0], $pay_time[1]]];
                }
            }
            //获取列表
            $_list = Db::name('order')->alias('o')
                ->join('member m', 'o.member_id=m.id')
                ->join('business b', 'o.business_id=b.id')
                ->field('o.*,m.nickname,m.avatarUrl,b.province,b.city,b.area,b.address,b.business_name')
                ->where($where)
                ->page($page, $limit)
                ->order('o.create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['bus_address'] = $v['province'] . $v['city'] . $v['area'] . $v['address'];
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($_list[$k]['pay_time']) {
                    $_list[$k]['pay_time'] = date('Y-m-d H:i:s', $v['pay_time']);
                } else {
                    $_list[$k]['pay_time'] = "------";
                }
                //订单编号
                // 用户名
                // 用户图片
                // 所属商家 创建时间
                // 订单状态
                // 支付状态
                // 支付时间
                // 订单金额
                // 实际金额
            }
            //获取数量
            $total = Db::name('order')->alias('o')
                ->where($where)
                ->join('member m', 'o.member_id=m.id')
                ->join('business b', 'o.business_id=b.id')
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $area_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        foreach ($area_list as $k => $v) {
            $proviec[$v['id']] = $v['name'];
        }
        $this->assign('proviec', $proviec);
        return $this->fetch();
    }

    public function getc_cate()
    {
        $id = input('id');
        $query = db('area')->where('pid', $id)->select();
        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }

    //获取订单香型
    public function edit()
    {
        //  购买商品 商品图片
        // 订单价格 优惠额度
        //  实际支付 核销状态
        // 是否核销 核销时间
        $id = $this->request->param('id/d', 0);
        $order_info = Db::name('order')->find($id);
        $goods_info = Db::name('goods')->find($order_info['goods_id']);
        if ($order_info['write_time']) {
            $order_info['write_time'] = date('Y-m-d H:i:s', $order_info['write_time']);
        } else {
            $order_info['write_time'] = '待取货';
        }
        if ($order_info['pay_time']) {
            $order_info['pay_time'] = date('Y-m-d H:i:s', $order_info['pay_time']);
        }
        if ($order_info['tui_time']) {
            $order_info['tui_time'] = date('Y-m-d H:i:s', $order_info['tui_time']);
        }

        $this->assign([
            'order_info' => $order_info,
            'goods_info' => $goods_info
        ]);
        return $this->fetch();
    }

    public function refund()
    {
        $order_id = $this->request->param('id/d', 0);
        Db::startTrans();
        try {
            $order_info = Db::name('order')->where('id', $order_id)->find();
            $config = config('wechat.wxpay');
            $app = Factory::payment($config);
            $result = $app->refund->byOutTradeNumber($order_info['order_no'], $order_info['order_no'], $order_info['pay_money'] * 100, $order_info['pay_money'] * 100, [
                // 可在此处传入其他参数，详细参数见微信支付文档
                'refund_desc' => '订单退款',
            ]);
            if ($result['result_code'] == "FAIL") {
                return ['code' => 100, 'data' => '', 'msg' => $result['err_code_des']];
            }
            tuikuan($order_info, $order_id, '后台退款',1);
            Db::commit();
            return ['code' => 200, 'data' => '', 'msg' => '操作成功'];
        } catch (\Exception $e) {
            Db::rollback();
            return ['code' => 100, 'data' => '', 'msg' => '操作失败' . $e->getMessage()];
        }
    }


}